module.exports = app => {
    const { STRING, DATE, BIGINT, INTEGER, TEXT } = app.Sequelize;
    const order = app.model.define("order", {
        id: {
            type: BIGINT(11),
            autoIncrement: true,
            primaryKey: true,
            unique: true
        },
        state: {
            type: BIGINT(11),
            defaultValue: 0 // 0-待确认 1-已确定 2-待发货 3-已发货 4-已完成
        },
        openid: STRING(30), // 用户识别
        commodity_id: BIGINT(11), // 商品ID
        messages: TEXT, // 留言
        selectedNum: BIGINT(11), // 数量
        coupon_id: {
            type: BIGINT(11),
            defaultValue: 0
        }, // 优惠券ID
        address_id: {
            type: BIGINT(11),
            defaultValue: 0
        }, // 地址ID
        status: {
            type: INTEGER,
            defaultValue: 1
        },
        created_at: DATE,
        updated_at: DATE,
    }, {
        timestamps: true, // 自动维护时间戳 [ created_at、updated_at ]
        createdAt: 'created_at',
        updatedAt: 'updated_at',
        deletedAt: false, // 'deleted_at',
        paranoid: false
    });

    order.associate = () => {
        app.model.ModOrder.belongsTo(app.model.ModCommodity, {
            as: 'commodity',
            foreignKey: 'commodity_id',
            targetKey: 'id'
        });
    }

    // order.sync({ force: true });

    return order;
};